package com.hdaccp.foodsales.controller;

import com.hdaccp.foodsales.dao.PermissionDao;
import com.hdaccp.foodsales.dao.UserDao;
import com.hdaccp.foodsales.dao.UserPermissionDao;
import com.hdaccp.foodsales.dao.UserRoleDao;
import com.hdaccp.foodsales.entity.Permission;
import com.hdaccp.foodsales.entity.User;
import com.hdaccp.foodsales.entity.UserRole;
import com.hdaccp.foodsales.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
@Scope("prototype")
public class  UserInfoController {
    @Autowired
    private UserService userInfoService ;
    @Autowired
    private UserRoleDao userRoleDao;
    @Autowired
    private  PermissionDao permissionDao;
    @Autowired
    private UserDao UserDao;
    @Autowired
    private UserPermissionDao userPermissionDao;


    @RequestMapping("/login")
    public String login(String username, String pwd, Model model,boolean rm,HttpServletRequest request){

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token =new  UsernamePasswordToken(username,pwd,rm);
        try {
            subject.login(token);
            User user = UserDao.login(username,pwd);
            request.getSession().setAttribute("user",user);
            UserRole userRole = userRoleDao.getRoleByUserId((int) user.getUserid());
            List<Integer> list = userPermissionDao.getPermissionByRoleId((int) userRole.getGroupsid());
            List<Permission> list1 = permissionDao.queryByPermissionIds(list);

            List<Permission> list2 = new ArrayList<>();
            for (Permission p : list1) {
                if (p.getParentid() == 0) {
                    list2.add(p);
                }
            }
            request.getSession().getServletContext().setAttribute("list", list2);
            request.getSession().getServletContext().setAttribute("list1", list1);

            return "redirect:/main.jsp";

        }  catch (UnknownAccountException e) {
            e.printStackTrace();
            request.getSession().setAttribute("msg", "用户名不对！");
            return "redirect:/login.jsp";

        }  catch (IncorrectCredentialsException e){
            request.getSession().setAttribute("msg", "密码不对！");
            return "redirect:/login.jsp";
        } catch (AuthenticationException e) {
            e.printStackTrace();
            request.getSession().setAttribute("msg", "用户名或者密码不对！");
            return "redirect:/login.jsp";

        }
    }

    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        request.getSession().removeAttribute("user");
        //取消认证
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "redirect:/login.jsp" ;
    }

    @RequestMapping("/queryAll")
    @ResponseBody
    public Map<String,Object> queryAll(int offset, int limit){
       /* Map<String,Object> map = new HashMap<>() ;
        map.put("rows",userInfoService.findAll(offset,limit)) ;
        map.put("total",userInfoService.findTotalCount()) ;
        return  map ;*/

       return  null ;
    }

    @RequestMapping("/querySearch")
    @ResponseBody
        public Map<String,Object> querySearch(String username,int offset, int limit){

        Map<String,Object> map = new HashMap<>() ;
        map.put("rows",userInfoService.findAll(username,offset,limit)) ;
        map.put("total",userInfoService.findTotalCount(username)) ;
        return  map ;

    }


    @RequestMapping("/dofenpei")
    @ResponseBody
    public String dofenpei(int userid,int Groupsid){
        Map<String,Object> map =new HashMap<>();
        userRoleDao.updata(userid,Groupsid);
        return "";
    }


    @RequestMapping("/zja")
    @ResponseBody
    public int zja(User user){
        return UserDao.zj(user);
    }

}
